package co.za.itweb.brainstormlite;


import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.json.JSONArray;
import org.json.JSONException;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class UserData extends Activity{
	private TextView user;
	private TextView expire;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		requestWindowFeature( Window.FEATURE_NO_TITLE );
		super.onCreate(savedInstanceState);
		setContentView(R.layout.user);
		
		Button close = (Button) findViewById(R.id.close);
		Button login = (Button) findViewById(R.id.btn_login);
		 user= (TextView) findViewById(R.id.username);
		 expire= (TextView) findViewById(R.id.expire);
		 String userdata=checkUserLogin();
		 if(userdata.length()>2){
		 String[] separated = userdata.split("\\|");
		 
		
			 user.setText("Username : "+separated[0]); 
			 expire.setText("Valid untill : "+separated[1]);
			 if(separated[2].equals("expired")){
				 Toast.makeText(UserData.this, "Your subscription has expired ! ", Toast.LENGTH_LONG).show();
			 }
		 }else{
			 Toast.makeText(getApplicationContext(), "No login data present, please log-in first !", Toast.LENGTH_SHORT).show();
				finish();
		 }
		close.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent returnIntent = new Intent();
				returnIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				setResult(111, returnIntent);
				finish();
			}
		});
		
		login.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				
				        		  SharedPreferences settings = getSharedPreferences(MenuActivity.LOGIN_NAME, 0);
				   				SharedPreferences.Editor editor = settings.edit();
				   				editor.clear();
				   				editor.commit();
				   				SingleNews.logged=false;
				   				Toast.makeText(getApplicationContext(), "You are now logged-out !", Toast.LENGTH_SHORT).show();
				   				finish();
				         
			}
		});
	}
	
	
	public String checkUserLogin() {
		String result = "";
		try {
			SharedPreferences myPrefs = getSharedPreferences(
					MenuActivity.LOGIN_NAME, MODE_WORLD_READABLE);
			String prefName = myPrefs.getString("co.za.brainstormlite.login",
					"login");
			prefName = prefName.replaceAll("/", "");

			JSONArray dat;
			dat = new JSONArray(prefName);

			for (int t = 0; t < dat.length(); t++) {
				try {
					String obj = dat.getJSONObject(t).getString("user");

					if (obj.length() > 0) {
						
						String ex = dat.getJSONObject(t).getString("expiry");
						result=obj+"|"+ex.substring(0,10);
							if (!compareDates(ex.substring(0,10))) {
								
								result+="|expired";
							} else {
								
								result+="|valid";
							}
						
						

					}

				} catch (JSONException e) {
					e.printStackTrace();

				}
			}
		} catch (JSONException e1) {
			e1.printStackTrace();

		}

		return result;

	}
	
	public boolean compareDates(String givenDateString) {
		Calendar cal = Calendar.getInstance();
		boolean result=false;
		Calendar currentcal = Calendar.getInstance();
		String[] separated = givenDateString.substring(0,10).split("-");
		  cal.set(Integer.parseInt(separated[0]),Integer.parseInt(separated[1]),Integer.parseInt(separated[2]));
		  currentcal.set(currentcal.get(Calendar.YEAR),
		currentcal.get(Calendar.MONTH), currentcal.get(Calendar.DAY_OF_MONTH));
		  if(cal.before(currentcal)){
		  System.out.print("Current date(" + new SimpleDateFormat("yyyy-MM-dd").
		format(currentcal.getTime()) + ") is greater than the given date " + new
		SimpleDateFormat("yyyy-MM-dd").format(cal.getTime()));
		  result=false;
		  }else if(cal.after(currentcal)){
		  System.out.print("Current date(" + new SimpleDateFormat("yyyy-MM-dd").
		format(currentcal.getTime()) + ") is less than the given date " + new 
		SimpleDateFormat("yyyy-MM-dd").format(cal.getTime()));
		  result=true;
		  }else{
		  System.out.print("Both date are equal.");
		  result=true;
		  }
		  
		  return result;
	}

}
